//
//  YWActionSheet.m
//  YWActionSheet
//
//  Created by suzhizhan on 15/7/24.
//
//

#import "YWActionSheet.h"
#import "UIView+XIB.h"
#import "SheetTableViewCell.h"
@implementation YWActionSheet


-(id)initWithlist:(NSArray *)list height:(CGFloat)height andCancelBtn:(NSString *)cancleName{
    self = [super init];
    if(self){
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.backgroundColor = RGBACOLOR(160, 160, 160, 0);
        self.titleList = list;
        [self initBgViewWithListCount:list.count];
        [self initCancleButtonWithListCount:list.count andBtnName:cancleName];
        [self initTableViewWithList:list];
     //   [self initLineView];
        [self animeData];
    }
    return self;
}

- (void)initCancleButtonWithListCount:(NSInteger)count andBtnName:(NSString *)cancleName{
    cancleBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancleBtn.frame=CGRectMake(0, 2+count*DefaultHeight+1, ScreenWidth, 42);
    cancleBtn.backgroundColor=[UIColor colorWithARGB:0xffffff];
    [cancleBtn setTitle:cancleName forState:UIControlStateNormal];
    [cancleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(clickCaccleBtn) forControlEvents:UIControlEventTouchUpInside];
    //cancleBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [backGroundView addSubview:cancleBtn];
}

- (void)initLineView{
    lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 2)];
    lineView.backgroundColor=[UIColor colorWithARGB:0xff630d];
    [backGroundView addSubview:lineView];
}

- (void)initBgViewWithListCount:(NSInteger)count{
    backGroundView=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth,DefaultHeight*count+45)];
    backGroundView.backgroundColor=[UIColor colorWithARGB:0xc8c0bd];
    [self addSubview:backGroundView];
}

- (void)clickCaccleBtn{
    if(_delegate!=nil && [_delegate respondsToSelector:@selector(actionSheetClickedCancleBtnWithActionSheet:)]){
        [self.delegate actionSheetClickedCancleBtnWithActionSheet:self];
    }
    [self tappedCancel];
}

- (void)initTableViewWithList:(NSArray *)list{
    view = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,DefaultHeight*([list count])) style:UITableViewStylePlain];
    listData = list;
    view.dataSource = self;
    view.delegate = self;
    view.scrollEnabled = NO;
    view.separatorStyle=UITableViewCellSeparatorStyleNone;
    view.backgroundColor=[UIColor whiteColor];
    [backGroundView addSubview:view];
}

- (void)animeData{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
    [self addGestureRecognizer:tapGesture];
    tapGesture.delegate = self;
    [UIView animateWithDuration:.25 animations:^{
        self.backgroundColor = RGBACOLOR(0, 0, 0, .4);
        [UIView animateWithDuration:.25 animations:^{
            [backGroundView setFrame:CGRectMake(backGroundView.frame.origin.x, ScreenHeight - backGroundView.frame.size.height, backGroundView.frame.size.width, backGroundView.frame.size.height)];
        }];
    } completion:^(BOOL finished) {
    }];

}

-(void)tappedCancel{
    [UIView animateWithDuration:.25 animations:^{
        [view setFrame:CGRectMake(0, ScreenHeight,ScreenWidth, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if([touch.view isKindOfClass:[self class]]){
        return YES;
    }
    return NO;
}

- (void)showinView:(UIView *)showView{
    self.frame = showView.bounds;
    [showView addSubview:self];
}

-(void)showInView:(UIViewController *)Sview andIsRootVc:(BOOL)isRoot{
    [Sview.view endEditing:YES];
    if(Sview!=nil && isRoot){
        [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
    }else{
        //[view addSubview:self];
        [Sview.view addSubview:self];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return listData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DefaultHeight;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=nil;
    NSString *string=@"SheetTableViewCell";
    cell=[tableView dequeueReusableCellWithIdentifier:string];
    if (!cell){
        cell=[SheetTableViewCell instanceWithNibName:string owner:nil];
    }
    ((SheetTableViewCell*)cell).mLabel.text=listData[indexPath.row];
   // [(SheetTableViewCell*)cell setWithData:@[]];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
   //cell.textLabel.text=listData[indexPath.row];
   // cell.contentView.backgroundColor=[UIColor orangeColor];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self tappedCancel];
    if(_delegate!=nil && [_delegate respondsToSelector:@selector(actionSheetClickedAtIndex:andActionSheet:)]){
        [self.delegate actionSheetClickedAtIndex:indexPath.row andActionSheet:self];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
